﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabMonitorRemote
{
    public partial class DewkMeasurmentUserControl : JunkControl
    {
        private DewkMeasurment _Measurment;
        private DewkLogger _Dewk;
        private Boolean _DisplayF;

        public DewkMeasurmentUserControl(DewkMeasurment Measurment, DewkLogger Dewk)
        {
            InitializeComponent();
            DewKMeasurment = Measurment;
            DewK = Dewk;
            SubscribeToMeasurment();
            // DisplayInitiating();
        }

        private void SubscribeToMeasurment()
        {
            DewKMeasurment.MeasurmentComplete += new DewkMeasurment.MeasurmentCompleteEventHandler(RespondToMeasurmentComplete);
            DewKMeasurment.MeasurmentStarting += new DewkMeasurment.MeasurmentStartingEventHandler(RespondToMeasurmentStarting);
            DewKMeasurment.MeasurmentFailed += new DewkMeasurment.MeasurmentFailedEventHandler(RespondToMeasurmentFailed);
        }

        public DewkMeasurment DewKMeasurment
        {
            get { return _Measurment; }
            set { _Measurment = value; }
        }

        public DewkLogger DewK
        {
            get { return _Dewk; }
            set { _Dewk = value; }
        }
        public Boolean DisplayFahrenheit
        {
            get { return _DisplayF; }
            private set
            {
                if (_DisplayF != value)
                    _DisplayF = value;
            }
        }

        //On Complete
        private void RespondToMeasurmentComplete(object sender, EventArgs e)
        {
            DisplayFahrenheit = this._Measurment.DisplayF;
            DisplayMeasurments();
        }
        //On Starting
        private void RespondToMeasurmentStarting(object sender, EventArgs e)
        {
            DisplayStarting();
        }
        //On Failed
        private void RespondToMeasurmentFailed(object sender, EventArgs e)
        {
            DisplayFailed();
        }

        private string TempReading(double TempValue)
        {
            char degSymbol = (char)176;
            string scale;
            double temperature;
            string reading;
            if (DisplayFahrenheit)
            {
                scale = "F";
            }
            else
            {
                scale = "C";
            }
            temperature = TempValue;
            reading = string.Format("{0:N2}{1} {2}",temperature,degSymbol, scale);
            return reading;
        }

        private string HumidityReading(double humidity)
        {
            string reading;
            reading = string.Format("{0} % R.H.", humidity);
            return reading;
        }

        private void DisplayMeasurments()
        {
            this.Invoke((Action)delegate
            {
                //readings
                this.lblCh1Temp.Text = TempReading(DewKMeasurment.Ch1Temperature);
                this.lblCh1Humid.Text = HumidityReading(DewKMeasurment.Ch1Humidity);
                this.lblCh2Temp.Text = TempReading(DewKMeasurment.Ch2Temperature);
                this.lblCh2Humid.Text = HumidityReading(DewKMeasurment.Ch2Humidity);

                this.lblLastUpdate.Text = DewKMeasurment.MeasurmentTime.ToShortTimeString();
            });
        }
        private void DisplayFailed()
        {
            this.Invoke((Action)delegate
            {
                // UPDATE FAILED
                this.lblLoggerName.Text = DewK.LoggerName;
                this.lblIPAddress.Text = DewK.IpAddress;

                // channel headings
                this.lblChannel1.ForeColor = Color.Red;
                this.lblChannel2.ForeColor = Color.Red;

                //readings
                this.lblCh1Temp.Text = "FAILED";
                this.lblCh1Humid.Text = "FAILED";
                this.lblCh2Temp.Text = "FAILED";
                this.lblCh2Humid.Text = "FAILED";

                this.lblCh1Temp.ForeColor = Color.Red;
                this.lblCh1Humid.ForeColor = Color.Red;
                this.lblCh2Temp.ForeColor = Color.Red;
                this.lblCh2Humid.ForeColor = Color.Red;


                this.lblLastUpdate.Text = "FAILED";
            });
        }
        private void DisplayStarting()
        {
            this.Invoke((Action)delegate
            {
                this.lblLoggerName.Text = DewK.LoggerName;
                this.lblIPAddress.Text = DewK.IpAddress;

                //readings
                this.lblCh1Temp.Text = "";
                this.lblCh1Humid.Text = "";
                this.lblCh2Temp.Text = "";
                this.lblCh2Humid.Text = "";

                this.lblLastUpdate.Text = "Getting Reading...";

            });
        }

        private void DewkMeasurmentUserControl_Load(object sender, EventArgs e)
        {

        }

        /*
        private void DisplayInitiating()
        {
            
            this.Invoke((Action)delegate
            {
                this.lblLoggerName.Text = DewK.LoggerName;
                this.lblIPAddress.Text = DewK.IpAddress;
                
                //readings
                this.lblCh1Temp.Text = "";
                this.lblCh1Humid.Text = "";
                this.lblCh2Temp.Text = "";
                this.lblCh2Humid.Text = "";

                this.lblLastUpdate.Text = "Initializing";
                
            });
        }
        */
    }
}
